package org.alwayssuper.protocol.commons.transform.realtimev2016;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 2016版极值数据格式 (0x06)
 * 参考AICoding报文规则文档
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class ExtremeData {
    @Field(length = 1, desc = "最高电压电池子系统号:1~250,0xFE异常,0xFF无效")
    private int maxVoltageBatterySubsystemNo;
    
    @Field(length = 1, desc = "最高电压电池单体代号:1~252,0xFE异常,0xFF无效")
    private int maxVoltageBatteryCellNo;
    
    @Field(length = 2, desc = "电池单体电压最高值:0~15000(0~15V),精度0.001V,0xFFFF无效")
    private int maxBatteryCellVoltage;
    
    @Field(length = 1, desc = "最低电压电池子系统号:1~250,0xFE异常,0xFF无效")
    private int minVoltageBatterySubsystemNo;
    
    @Field(length = 1, desc = "最低电压电池单体代号:1~252,0xFE异常,0xFF无效")
    private int minVoltageBatteryCellNo;
    
    @Field(length = 2, desc = "电池单体电压最低值:0~15000(0~15V),精度0.001V,0xFFFF无效")
    private int minBatteryCellVoltage;
    
    @Field(length = 1, desc = "最高温度子系统号:1~250,0xFE异常,0xFF无效")
    private int maxTempSubsystemNo;
    
    @Field(length = 1, desc = "最高温度探针序号:1~252,0xFE异常,0xFF无效")
    private int maxTempProbeNo;
    
    @Field(length = 1, desc = "最高温度值:-40~210℃,偏移40℃,0xFF无效")
    private int maxTemperature;
    
    @Field(length = 1, desc = "最低温度子系统号:1~250,0xFE异常,0xFF无效")
    private int minTempSubsystemNo;
    
    @Field(length = 1, desc = "最低温度探针序号:1~252,0xFE异常,0xFF无效")
    private int minTempProbeNo;
    
    @Field(length = 1, desc = "最低温度值:-40~210℃,偏移40℃,0xFF无效")
    private int minTemperature;
    
    // =========================== 传输值与真实值转换方法 ===========================
    
    /**
     * 获取最高电池单体电压值 (V)
     * 传输值范围: 0~15000, 表示 0V~15V, 精度 0.001V
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getMaxBatteryCellVoltage() {
        if (maxBatteryCellVoltage == 0xFFFE || maxBatteryCellVoltage == 0xFFFF) {
            return Double.NaN;
        }
        return maxBatteryCellVoltage * 0.001;
    }
    
    /**
     * 设置最高电池单体电压值 (V)
     * 值范围: 0V~15V
     */
    public void setMaxBatteryCellVoltage(double maxBatteryCellVoltage) {
        if (Double.isNaN(maxBatteryCellVoltage) || maxBatteryCellVoltage < 0 || maxBatteryCellVoltage > 15.0) {
            this.maxBatteryCellVoltage = 0xFFFF; // 无效
        } else {
            this.maxBatteryCellVoltage = (int) Math.round(maxBatteryCellVoltage * 1000);
        }
    }
    
    /**
     * 获取最低电池单体电压值 (V)
     * 传输值范围: 0~15000, 表示 0V~15V, 精度 0.001V
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getMinBatteryCellVoltage() {
        if (minBatteryCellVoltage == 0xFFFE || minBatteryCellVoltage == 0xFFFF) {
            return Double.NaN;
        }
        return minBatteryCellVoltage * 0.001;
    }
    
    /**
     * 设置最低电池单体电压值 (V)
     * 值范围: 0V~15V
     */
    public void setMinBatteryCellVoltage(double minBatteryCellVoltage) {
        if (Double.isNaN(minBatteryCellVoltage) || minBatteryCellVoltage < 0 || minBatteryCellVoltage > 15.0) {
            this.minBatteryCellVoltage = 0xFFFF; // 无效
        } else {
            this.minBatteryCellVoltage = (int) Math.round(minBatteryCellVoltage * 1000);
        }
    }
    
    /**
     * 获取最高温度值 (℃)
     * 传输值范围: 0~250, 偏移量40℃, 表示 -40℃~+210℃, 精度 1℃
     * 0xFE: 异常, 0xFF: 无效
     */
    public double getMaxTemperature() {
        if (maxTemperature == 0xFE || maxTemperature == 0xFF) {
            return Double.NaN;
        }
        return maxTemperature - 40.0;
    }
    
    /**
     * 设置最高温度值 (℃)
     * 值范围: -40℃~+210℃
     */
    public void setMaxTemperature(double maxTemperature) {
        if (Double.isNaN(maxTemperature) || maxTemperature < -40.0 || maxTemperature > 210.0) {
            this.maxTemperature = 0xFF; // 无效
        } else {
            this.maxTemperature = (int) Math.round(maxTemperature + 40);
        }
    }
    
    /**
     * 获取最低温度值 (℃)
     * 传输值范围: 0~250, 偏移量40℃, 表示 -40℃~+210℃, 精度 1℃
     * 0xFE: 异常, 0xFF: 无效
     */
    public double getMinTemperature() {
        if (minTemperature == 0xFE || minTemperature == 0xFF) {
            return Double.NaN;
        }
        return minTemperature - 40.0;
    }
    
    /**
     * 设置最低温度值 (℃)
     * 值范围: -40℃~+210℃
     */
    public void setMinTemperature(double minTemperature) {
        if (Double.isNaN(minTemperature) || minTemperature < -40.0 || minTemperature > 210.0) {
            this.minTemperature = 0xFF; // 无效
        } else {
            this.minTemperature = (int) Math.round(minTemperature + 40);
        }
    }
    
    /**
     * 检查最高电压电池子系统号是否异常
     */
    public boolean isMaxVoltageBatterySubsystemNoAbnormal() {
        return maxVoltageBatterySubsystemNo == 0xFE;
    }
    
    /**
     * 检查最高电压电池子系统号是否无效
     */
    public boolean isMaxVoltageBatterySubsystemNoInvalid() {
        return maxVoltageBatterySubsystemNo == 0xFF;
    }
    
    /**
     * 检查最高电压电池单体代号是否异常
     */
    public boolean isMaxVoltageBatteryCellNoAbnormal() {
        return maxVoltageBatteryCellNo == 0xFE;
    }
    
    /**
     * 检查最高电压电池单体代号是否无效
     */
    public boolean isMaxVoltageBatteryCellNoInvalid() {
        return maxVoltageBatteryCellNo == 0xFF;
    }
    
    /**
     * 检查最高电池单体电压是否异常
     */
    public boolean isMaxBatteryCellVoltageAbnormal() {
        return maxBatteryCellVoltage == 0xFFFE;
    }
    
    /**
     * 检查最高电池单体电压是否无效
     */
    public boolean isMaxBatteryCellVoltageInvalid() {
        return maxBatteryCellVoltage == 0xFFFF;
    }
    
    /**
     * 检查最高温度是否异常
     */
    public boolean isMaxTemperatureAbnormal() {
        return maxTemperature == 0xFE;
    }
    
    /**
     * 检查最高温度是否无效
     */
    public boolean isMaxTemperatureInvalid() {
        return maxTemperature == 0xFF;
    }
}